package com.elective.courseselect.controller;

import com.alibaba.fastjson.JSONObject;
import com.elective.courseselect.entity.CourseEntity;
import com.elective.courseselect.entity.TeacherEntity;
import com.elective.courseselect.service.CourseService;
import com.elective.courseselect.service.TeacherService;
import com.elective.courseselect.serviceUtils.CourseUtils;
import com.elective.courseselect.serviceUtils.UserUtils;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author lee
 * @email 943204006@qq.ccom
 * @date 2021-09-04 19:23:06
 */
@RestController
@RequestMapping("courseselect/course")
@CrossOrigin(origins = "*",maxAge = 3600)
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private TeacherService teacherService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("查看所有课程列表")
    public R list(@RequestParam Map<String, Object> params ,  CourseEntity courseEntity){
        PageUtils page = courseService.queryPage(params,courseEntity);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		CourseEntity course = courseService.getById(id);
        Integer teacherId = course.getTeacherId();
        TeacherEntity teacher = teacherService.searchTeacherInfos(teacherId);
        course.setTeacherName(teacher.getName());
        Boolean abandon = UserUtils.isAbandon(course.getTeacherId());
        if (abandon) {
            return R.error(StatusCode.ERROR,"该课程老师账号已被弃用");
        }
        CourseUtils.getCourseLabels(course);
        CourseUtils.getCourseComment(course);
        course.setScheduleByJson((JSONObject) JSONObject.parse(course.getSchedule()));
        return R.ok().put("course", course);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CourseEntity course){
		courseService.save(course);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CourseEntity course){
		courseService.updateById(course);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		courseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 信息
     */
    @GetMapping("/searchCourseInfo/{courseId}")
    @ApiOperation("查看课程详情信息")
    public R searchCourseInfo(@PathVariable("courseId") Integer courseId){
        CourseEntity course = courseService.searchCourseInfo(courseId);
        return R.ok().put("course", course);
    }


    /**
     * 修改
     */
    @ApiOperation("修改课程基本信息")
    @PostMapping("/updateCourse")
    public R updateCourse(@RequestBody CourseEntity course){
        courseService.updateCourse(course);
        return R.ok(StatusCode.OK,"修改课程基本信息成功");
    }

    /**
     * 添加课程
     */
    @ApiOperation("添加课程")
    @PostMapping("/createCourse")
    public R createCourse(@RequestBody CourseEntity course){
        courseService.createCourse(course);
        return R.ok(StatusCode.OK,"添加课程成功");
    }

    /**
     * 删除课程
     */
    @ApiOperation("删除课程")
    @PostMapping("/delCourse")
    public R delCourse(@RequestBody Integer[] courseIds){
        courseService.delCourse(courseIds);
        return R.ok(StatusCode.OK,"删除课程成功");
    }


    /**
     * 信息
     */
    @GetMapping("/searchCourseByModifyList")
    @ApiOperation("查看课程详情信息")
    public R searchCourseListByModify(@RequestParam Map<String, Object> params ,  CourseEntity courseEntity){
        PageUtils page = courseService.searchCourseByModifyList(params,courseEntity);
        return R.ok().put("page", page);
    }

    /**
     * 审核课程信息修改
     */
    @PostMapping("/auditCourse")
    @ApiOperation("审核课程信息修改")
    public R auditCourse(@RequestBody CourseEntity courseEntity){
        return  courseService.auditCourse(courseEntity);
    }




}
